<style>
    .treeTable-icon .layui-icon-layer:before {
        content: "\e638";
    }

    /** 展开 */
    .treeTable-icon.open .layui-icon-layer:before {
        content: "\e638";
    }

    .treeTable-icon .layui-icon-file:before {
        content: "\e621";
    }

    /** 未展开 */
    .treeTable-icon .layui-icon-triangle-d:before {
        content: "\e623";
    }

    /** 展开 */
    .treeTable-icon.open .layui-icon-triangle-d:before {
        content: "\e625";
    }
</style>
<div class="fsh-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-btn-group">
            <button class="layui-btn" id="btn-expand">全部展开</button>
            <button class="layui-btn" id="btn-fold">全部折叠</button>
            <button class="layui-btn" id="btn-refresh">刷新表格</button>
        </div>
        <div class="layui-btn-container" style="text-align: right">
            <button class="layui-btn btn-primary" id="menu_add">新建分类</button>
        </div>
        <table id="auth-table" class="layui-table" lay-filter="mainList"></table>
    </div>
</div>


<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var tableObj;
        // 渲染表格
        layer.load(2);
        var renderTable = function () {
            tableObj = treetable.render({
                treeColIndex: 0,
                treeSpid: '#',
                treeIdName: 'major_id',
                treePidName: 'parent',
                elem: '#auth-table',
                url: context + '/api/edumajor/getList?tree=false',
                page: false,
                treeDefaultClose: false,
                cols: [[ //表头
                    {field: 'title', title: '分类名称', align: 'left', width: 300},
                    {field: 'major_id', title: '分类ID', align: 'left', width: 300},
                    {field: 'subject', title: '是否必修', align: 'center', templet: function (d) {
                            return d.subject===0?'否':'是';
                        }},
                    {field: 'sort', title: '排序', align: 'center'},
                    {
                        field: 'lock', title: '操作', width: 200, align: 'center', unresize: true,
                        templet: function (d) {
                            return '<a href="javascript:;" class="font-primary" lay-event="update">修改</a>'
                                + '<a href="javascript:;" class="font-primary" lay-event="del">删除</a>';
                        }
                    }
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        }

        renderTable();
        $('#btn-expand').click(function () {
            treetable.expandAll('#auth-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#auth-table');
        });
        $('#btn-refresh').click(function () {
            renderTable();
        });
        $('#btn-search').click(function () {
            var keyword = $('#edt-search').val();
            var searchCount = 0;
            $('#auth-table').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
                $(this).css('background-color', 'transparent');
                var text = $(this).text();
                if (keyword != '' && text.indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        treetable.expandAll('#auth-table');
                        $('html,body').stop(true);
                        $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (keyword == '') {
                layer.msg("请输入搜索内容", {icon: 5});
            } else if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            }
        });

        // 添加
        $("#menu_add").click(function () {
            showDialog({
                title: '新建分类'
                , template: 'desc.html'
                , tableObj: tableObj
                , yes: function (index, layero) {
                    layui.form.on('submit(*)', function (data) {

                        $.ajax({
                            url: context + '/api/edumajor/insertOne',
                            type: 'POST',
                            async: false,
                            dataType: 'json',
                            contentType: "application/json;charset-UTF-8",
                            data: JSON.stringify(data.field),
                            success: function (result) {
                                if (result.success) {
                                    layer.msg("添加成功");
                                    layer.close(index);
                                    renderTable();
                                } else {
                                    layer.msg("添加失败");
                                }
                            },
                            error: function () {
                                layer.msg("添加异常");
                            }
                        })
                    });

                    //表单验证
                    layui.form.validate();//默认form名称为add_form（可不填写）
                }

            })
        });

        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，mainList是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据

            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'update') {
                showDialog({
                    title: '编辑分类'
                    , template: 'edit'
                    , tableObj: tableObj
                    , htmlData: data
                    , yes: function (index, layero) {
                        layui.form.on('submit(*)', function (data) {

                            $.ajax({
                                url: context + '/api/edumajor/updateOne',
                                type: 'PATCH',
                                async: false,
                                dataType: 'json',
                                contentType: "application/json;charset-UTF-8",
                                data: JSON.stringify(data.field),
                                success: function (result) {
                                    if (result.success) {
                                        layer.msg("修改成功");
                                        layer.close(index);
                                        renderTable();
                                    } else {
                                        layer.msg("修改失败");
                                    }
                                },
                                error: function () {
                                    layer.msg("修改异常");
                                }
                            })
                        });

                        //表单验证
                        layui.form.validate();//默认form名称为add_form（可不填写）
                    }

                })
            }
            // 删除
            if (layEvent === 'del') {
                layer.confirm('确认删除此帐号？', {
                    icon: 7,
                    title: "提示",
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    $.ajax({
                        url: context + '/api/edumajor/' + data.major_id,
                        type: "DELETE",
                        success: function (result) {
                            if (result.success && result.data > 0) {
                                layer.msg("删除成功");
                                renderTable();
                            } else {
                                layer.msg("删除失败");
                            }
                        },
                        error: function () {
                            layer.msg("删除异常");
                        }
                    })
                }, function () {
                    layer.closeAll();
                });
            }


        });
    });
</script>

